﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Media.Imaging;
using System.IO;

namespace CPAScoringPerformanceTester.Converters
{
    public enum ImageConverterType
    { 
        Memory,
        Url
    }

    public class ImageConverter : IValueConverter
    {
        public ImageConverterType ConverterType { get; set; }

        public ImageConverter()
        {
            ConverterType = ImageConverterType.Memory;
        }

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return value;

            if (ConverterType == ImageConverterType.Memory)
            {
                byte[] imgArray = value as byte[];
                BitmapImage image = new BitmapImage();
                image.BeginInit();
                image.StreamSource = new MemoryStream(imgArray);
                image.EndInit();

                return image;
            }
            else
            {
                return new BitmapImage(new Uri(value.ToString(), UriKind.RelativeOrAbsolute));
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
    }
}
